﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_10
{
    public partial class Form1 : Form
    {
        int[] numbers;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (!int.TryParse(txtCount.Text, out int count) || count <= 0)
            {
                MessageBox.Show("请输入有效的数字个数（正整数）");
                return;
            }
            if (!int.TryParse(txtMin.Text, out int min) || !int.TryParse(txtMax.Text, out int max) || max <= min)
            {
                MessageBox.Show("请输入有效的范围（最大值 > 最小值）");
                return;
            }

            // 生成随机数
            numbers = new int[count];
            Random random = new Random();
            for (int i = 0; i < count; i++)
            {
                numbers[i] = random.Next(min, max + 1); // 包含最大值
            }

            // 统计并显示结果
            ShowResult();
            btnSort.Enabled = true; // 启用排序按钮
        }

        // 排序按钮点击事件（切换正向/逆向排序）
        private void btnSort_Click(object sender, EventArgs e)
        {
            if (numbers == null) return;

            if (btnSort.Text == "正向排序")
            {
                Array.Sort(numbers);
                btnSort.Text = "逆向排序";
            }
            else
            {
                Array.Reverse(numbers);
                btnSort.Text = "正向排序";
            }

            ShowResult(); // 重新显示排序后的结果
        }

        // 显示随机数及统计信息
        private void ShowResult()
        {
            if (numbers == null) return;

            int positive = 0, negative = 0, even = 0, odd = 0;
            string numList = "";

            // 遍历数组统计并拼接数字列表
            foreach (int num in numbers)
            {
                numList += num + " ";
                if (num > 0) positive++;
                else if (num < 0) negative++;

                if (num % 2 == 0) even++;
                else odd++;
            }

            // 显示到多行文本框
            txtResult.Text = $"生成的随机数：{numList}\n\n" +
                           $"统计结果：\n" +
                           $"正数：{positive} 个\n" +
                           $"负数：{negative} 个\n" +
                           $"偶数：{even} 个\n" +
                           $"奇数：{odd} 个";
        }
    }
}
